#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash manta-1.6.0.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1

buildpath=$1
installpath=$2
[[ "$buildpath" =~ .*/$ ]] && buildpath=${buildpath%/*}
[[ "$installpath" =~ .*/$ ]] && installpath=${installpath%/*}
[[ "$buildpath" == "$installpath" ]] && echo "构建路径与安装路径需为不同路径" && exit 1
yum install -y  environment-modules csh time patch libatomic perl-Data-Dumper autoconf automake libtool numactl binutils systemd-devel valgrind flex wget

set -e
yum install -y doxygen tar bzip2 libstdc++-static zlib zlib-devel graphviz cmake 

wget -t 0 -c -P $buildpath https://github.com/Illumina/manta/archive/refs/tags/v1.6.0.tar.gz 
tar -zxvf $buildpath/v1.6.0.tar.gz -C $buildpath && rm -rf $buildpath/v1.6.0.tar.gz
cd $buildpath/manta-1.6.0
mkdir build && cd build
CC=clang CXX=clang++ ../configure --jobs=$(nproc) --prefix=$installpath/manta-1.6.0
make -j$(nproc) install

cat>"$installpath/manta-1.6.0/manta_modulefiles"<<EOF
#%Module1.0
conflict manta
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set MANTA \$pwd
setenv MANTA \$MANTA
prepend-path PATH \$MANTA/bin
EOF

rm -rf $buildpath/manta-1.6.0


